package lz.controls.nick
{
	import com.common.utils.Reflection;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	/**
	 * 换页 
	 * @author Administrator
	 * 
	 */	
	public class PagePanel extends Sprite //implements IComponent
	{
		/**
		 * 皮肤 
		 */
		private var symbolSkin:MovieClip;
		/**
		 *最小值 为1 
		 */
		private static const MIN_VALUE:int = 1;
		/**
		 *向左 
		 */
		private var _left:DisplayObject;
		/**
		 *向右 
		 */
		private var _right:DisplayObject;
		/**
		 * 
		 */
		private var _pageNum:TextField;
		
		private var _min:int;
		private var _max:int;
		private var _callBack:Function;
		
		public function PagePanel()
		{
			super();
//			initUI();
		}
		/**
		 *"com.lz.commonUI.turnPage_white" 
		 * @param view
		 * 
		 */		
		public function initUI(view:Object=null):void
		{
			if(view == null)
			{
				symbolSkin = Reflection.createInstance("com.lz.commonUI.turnPage");
				this.addChild(symbolSkin);
			}else if(view is String)
			{
				symbolSkin = Reflection.createInstance( view as String);
				this.addChild(symbolSkin);
			}else if(view is MovieClip)
			{
				symbolSkin = view as MovieClip;
			}
			if(symbolSkin == null)return;
			
			_left = symbolSkin._pageLeft;
			_right = symbolSkin._pageRight;
			_pageNum = symbolSkin._pageNum;
			min = 1;
			max = 1;
			addEvtListener();
		}
		
		public function addEvtListener():void
		{
			_left.addEventListener(MouseEvent.CLICK,onLeftHandler);
			_right.addEventListener(MouseEvent.CLICK,onRightHandler);
			
		}
		/**
		 *向左 
		 * @param event
		 * 
		 */
		private function onLeftHandler(event:MouseEvent):void
		{
			if(min <= MIN_VALUE)return;
			min--;
			if(_callBack != null)_callBack("left");
		}
		/**
		 *向右 
		 * @param event
		 * 
		 */
		private function onRightHandler(event:MouseEvent):void
		{
			if(max <= min)return;
			min++;
			if(_callBack != null)_callBack("right");
		}
		public function removeEvtListener():void
		{
			_left.removeEventListener(MouseEvent.CLICK,onLeftHandler);
			_right.removeEventListener(MouseEvent.CLICK,onRightHandler);
		}
		
		public function destroyView():void
		{
			removeEvtListener();
			if(this.parent)this.parent.removeChild(this);
			_callBack =  null;
			_left = null;
			_right = null;
			symbolSkin = null;
		}

		/**
		 *最小值 
		 */
		public function get min():int
		{
			return _min;
		}

		/**
		 * @private
		 */
		public function set min(value:int):void
		{
			_min = value;
			setValue();
		}

		/**
		 *最大值 
		 */
		public function get max():int
		{
			return _max;
		}

		/**
		 * @private
		 */
		public function set max(value:int):void
		{
			_max = value;
			if(_max <=0)
				_max = 1;
			setValue();
		}
		
		private function setValue():void
		{
			_pageNum.text = min + "/" + max;
//			if(  _pageNum.textWidth + 3 < 45)
//			{
//				_pageNum.width = 45;
//			}else
//			{
//				_pageNum.width = _pageNum.textWidth + 3;
//			}
//			_right.x = _pageNum.x + _pageNum.width;
//			
//			
			
		}

		public function set callBack($callBack:Function):void
		{
			_callBack = $callBack;
		}

	}
}